package me.youline.dataServer.dao;

import java.sql.ResultSet;
import java.sql.SQLException;

import me.youline.dataServer.entity.socialActionMsg.MsgNotifyLatestRecord;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Component;

/**
 * 最新消息记录实体DAO
 * 
 * @author linhan
 *
 */
@Component
public class SocialLatestNotificationRecordDAO {

	@Autowired
	private JdbcTemplate jdbcTemplate;

	private static final String TABLE_NAME = "yline_social_notice_latest";

	private static final String INSERT_SQL = "insert into " + TABLE_NAME
			+ " (userId,latestNoticeId,isExistsNewNotice,unReadTotal)values(?,?,?,?)";

	private static final  String UPDATE_SQL = "update "
			+ TABLE_NAME
			+ " set latestNoticeId = ? , isExistsNewNotice = ? , unReadTotal = unReadTotal+1 where userId = ?";

	private static final String UPDATE_IS_NEW_FLAG = "update " + TABLE_NAME
			+ " set isExistsNewNotice = ? , unReadTotal= ? where userId = ?";

	private static final String QUERY_SQL = "select * from " + TABLE_NAME
			+ " where userId = ? ";
	
	private static final String QUERY_TOTAL_SQL = "select unReadTotal from " + TABLE_NAME+ " where userId = ?";
	
	private String DELETE_SQL = "delete from " + TABLE_NAME
			+ " where userId = ? ";

	private static RowMapper<MsgNotifyLatestRecord> ROW_MAPPER = new RowMapper<MsgNotifyLatestRecord>() {

		@Override 
		public MsgNotifyLatestRecord mapRow(ResultSet rs, int rowNum)
				throws SQLException {
			MsgNotifyLatestRecord r = new MsgNotifyLatestRecord();
			r.setExistsNewNotice(rs.getBoolean("isExistsNewNotice"));
			r.setUserId(rs.getLong("userId"));
			r.setUnReadTotal(rs.getInt("unReadTotal"));
			long latestId = rs.getLong("latestNoticeId");
			r.setLatestNoticeId(latestId == 0 ? null : latestId);
			return r;
		}
	};

	/**
	 * 根据用户id查询其是否存在最新的通知消息
	 * 
	 * @param userId
	 *            用户ID
	 * @return
	 */
	public boolean isExistLatestMsg(Long userId) {
		return this.getRecordByUserId(userId).isExistsNewNotice();
	}

	/**
	 * 重置用户消息通知记录，即表示用户已经获取了最新通知消息。
	 * 
	 * @param userId
	 *            用户id
	 * @param latestMsgId
	 *            最新消息ID
	 */
	public void resetUserNotificationRecord(Long userId) {
		jdbcTemplate.update(UPDATE_IS_NEW_FLAG, new Object[] { false, 0,userId });
	}

	/**
	 * 增加用户最新通知消息记录
	 * 
	 * @param userId
	 *            用户ID
	 */
	public void addUserSocialNotificationRecord(Long userId) {
		jdbcTemplate.update(INSERT_SQL, new Object[] { userId, null, false,0 });
	}
	
	
	/**
	 * 增加用户最新通知消息记录
	 * @param r 最新记录
	 */
	public void addUserSocialNotificationRecord(MsgNotifyLatestRecord r) {
		jdbcTemplate.update(INSERT_SQL, new Object[] { r.getUserId(), r.getLatestNoticeId(), r.isExistsNewNotice(), r.getUnReadTotal()});
	}

	/**
	 * 将当前消息记录设置为最新记录
	 * 
	 * @param userId
	 *            用户ID
	 * @param latestNotifyId
	 *            最新消息ID
	 */
	public int setAsLatestNotificationRecord(Long userId, Long latestNotifyId) {
		return jdbcTemplate.update(UPDATE_SQL, new Object[] { latestNotifyId, true,
				userId });
	}

	/**
	 * 根据用户ID获取消息通知最新记录
	 * 
	 * @param userId
	 * @return
	 */
	public MsgNotifyLatestRecord getRecordByUserId(Long userId) {
		try {
			return jdbcTemplate.queryForObject(QUERY_SQL, new Object[] { userId },
					ROW_MAPPER);
		} catch (EmptyResultDataAccessException e) {
			return null;
		}
	}
	
	/**
	 * 获取用户最新未读消息数
	 * 
	 * @param userId
	 * @return
	 */
	public int getUnReadTotalNum(Long userId){
		try {
			return jdbcTemplate.queryForObject(QUERY_TOTAL_SQL, new Object[] { userId },
					Integer.class);
		} catch (EmptyResultDataAccessException e) {
			return 0;
		}
	}

	/**
	 * 删除最新消息记录
	 * 
	 * @param userId
	 */
	protected void deleteRecord(Long userId) {
		jdbcTemplate.update(DELETE_SQL, new Object[] { userId });
	}

}
